package it.ipermemory;

import it.ipermemory.graphic.CustomPopup;
import it.ipermemory.logic.Thing;
import it.ipermemory.logic.Game;
import it.ipermemory.utilities.MediaControllerPlayer;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * Activity that takes care of reproducing a sound with the classic management
 * buttons.
 * 
 * @author Gianluca Carlesso (gianluca.carlesso@gmail.com)
 * @version 1.0
 */
public class SoundActivity extends Activity {

	/**
	 * Object MediaPlayer that manage a multimedia content
	 */
	private MediaPlayer mediaPlayer;

	/**
	 * Identifier of a Thing on which to play the sound
	 */
	private int thing_id;

	/**
	 * Object that manage the playback
	 */
	private MediaControllerPlayer mControllerPlayer;

	/**
	 * An object that initializes the sound screen
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sound);

		// Initialize MediaPlayer and sound
		thing_id = getIntent().getExtras().getInt(Game.GAME_THING_CARD);

		Thing thing = Game.parserXML.getThingById(thing_id);

		if (thing == null) {
			// If there is an error show a message error
			mexError();
		} else {

			// Prepare a sound playback
			int sound_file = getResources().getIdentifier(thing.getSound(),
					"raw", getPackageName());

			mediaPlayer = MediaPlayer.create(getApplicationContext(),
					sound_file);

			// When the media is ready I enable buttons
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				public void onPrepared(MediaPlayer mp) {
					((ImageButton) findViewById(R.id.play_button))
							.setEnabled(true);

					((ImageButton) findViewById(R.id.pause_button))
							.setEnabled(true);

				}
			});

			// I create my content manager
			mControllerPlayer = new MediaControllerPlayer(mediaPlayer,
					(ImageButton) findViewById(R.id.play_button),
					(ImageButton) findViewById(R.id.pause_button),
					(ImageButton) findViewById(R.id.stop_button),
					(TextView) findViewById(R.id.total_time),
					(TextView) findViewById(R.id.current_time));
		}

	}

	/**
	 * Stop playing the sound
	 * 
	 * @param view
	 *            object on which the click was performed
	 */
	public void stopSound(View view) {
		try {
			mControllerPlayer.stopMedia();
		} catch (IllegalStateException e) {
			mexError();
		}
	}

	/**
	 * Play/Pause the sound
	 * 
	 * @param view
	 *            object on which the click was performed
	 */
	public void playPauseSound(View view) {
		mControllerPlayer.playPause();
	}

	/**
	 * Destroys the activity and releases the resources occupied by the media
	 */
	public void onDestroy() {
		super.onDestroy();

		// Release memory of memory
		mControllerPlayer.releaseMedia();
	}

	/**
	 * Show a pop-up error
	 */
	private void mexError() {
		CustomPopup cp = new CustomPopup(SoundActivity.this, getResources()
				.getString(R.string.error_media_load)) {

			@Override
			public void onClick(View v) {
				this.dismiss();
				finish();
			}
		};
		cp.getNoButton().setVisibility(View.GONE);
		cp.show();
	}
}
